<template>
    <!-- 添加或修改文件分享对话框 -->
    <el-dialog :visible.sync="visible" :title="dialogData.title"
               center :close-on-click-modal="true"
               :close-on-press-escape="true"
               @closed="$emit('update:visible', false)"
               width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="路径">
          <file-upload v-model="form.uploadFile"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { getShareFile, addShareFile, updateShareFile } from "@/api/exam/shareFile";

export default {
  name: "ShareFileEdit",

    props: {
        visible: {
            type: Boolean,
            default: false
        },
        dialogData: {
            type: Object,
            default: false
        }
    },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
      this.reset();
      this.getData();
  },
  methods: {
    /** 查询文件分享列表 */
    getData(){
        if (this.dialogData.id){
        getShareFile(this.dialogData.id).then(response => {
        this.form = response.data;
    })}},
    // 取消按钮
    cancel() {
        this.$emit('handleClose', { component: 'dialogOpen' })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fileName: null,
        fileSize: null,
        uploadFile: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        type: null,
        md5: null
      };
      this.resetForm("form");
    },



    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShareFile(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.$emit('handleClose', { component: 'dialogOpen', get: true })
            });
          } else {
            addShareFile(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.$emit('handleClose', { component: 'dialogOpen', get: true })
            });
          }
        }
      });
    },


    }
};
</script>
